/**
 * some common functions
 */

// 获取当前用户信息
function getNowUser() {
	var user;
	$.ajax({
		url : 'user/getNowUser',
		type : "GET",
		async : false,
		data : {},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				console.log(data);
				user = data['result']['nowUser'];
				break;
			default:
				alert(data['msg']);
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return user;
}

function getFollowers(type, userid) {
	var res = []
	$.ajax({
		url : 'user/getFollowersByUserid',
		type : "GET",
		async : false,
		data : {
			type : type,
			pageNumber : 1,
			userid : userid
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				break;
			case 0:
				res = data['result']['concernUsers'];
				break;
			default:
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;
}

function getConcernUsers(type, userid) {
	var res = []
	$.ajax({
		url : 'user/getConcernUserById',
		type : "GET",
		async : false,
		data : {
			type : type,
			pageNumber : 1,
			userid : userid
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				break;
			case 0:
				res = data['result']['concernUsers'];
				break;
			default:

				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;
}

function getPostsOfTopic() {

	$.ajax({
		url : 'post/getPostsOfTopic',
		type : "GET",
		async : false,
		data : {},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case 0:
				res = data['result']['concernUsers'];
				break;
			default:
				alert(data['msg']);

				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;
}

function getFollowCounts(type) {
	var res = 0
	$.ajax({
		url : 'user/getFollowersTotalNumber',
		type : "GET",
		async : false,
		data : {
			type : type
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				break;
			case 0:
				res = data['result']['totalNum'];
				break;
			default:
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;

}

function getConcernCounts(type) {
	var res = 0
	$.ajax({
		url : 'user/getConcernUsersTotalNumber',
		type : "GET",
		async : false,
		data : {
			type : type
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				break;
			case 0:
				res = data['result']['totalNum'];
				break;
			default:
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;

}

function getPosts(topicId, pageNumber) {
	res = []
	$.ajax({
		url : 'post/getPostsOfTopic',
		type : "GET",
		async : false,
		data : {
			topicid : topicId,
			pageNumber : pageNumber
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				res = data['result']['posts'];
				break;
			default:
				alert(data['msg']);
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;
}

function getPostsOfUser(type, pageNumber, userid) {
	res = []
	$.ajax({
		url : 'post/getPostsOfUser',
		type : "GET",
		async : false,
		data : {
			type : type,
			pageNumber : pageNumber,
			userid : userid
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				res = data['result']['posts'];
				break;
			default:
				alert(data['msg']);
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;
}

// 查看某个用户点赞的帖子
function getUserAgreePosts(type, pageNumber, userid) {
	res = []
	$.ajax({
		url : 'post/getUserAgreePosts',
		type : "GET",
		async : false,
		data : {
			type : type,
			pageNumber : pageNumber,
			userid : userid
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				res = data['result']['posts'];
				break;
			default:
				alert(data['msg']);
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;
}

// 查看某个用户收藏的帖子
function getUserCollectPosts(type, pageNumber, userid) {
	res = []
	$.ajax({
		url : 'post/getUserCollectPosts',
		type : "GET",
		async : false,
		data : {
			type : type,
			pageNumber : pageNumber,
			userid : userid
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				res = data['result']['posts'];
				break;
			default:
				alert(data['msg']);
				break;
			}
		},
		error : function(err) {
			console.log(err);
		}
	});
	return res;
}

// 开始加载动画
function startLoader() {

	$('#loader').shCircleLoader({
		dots : 24,
		dotsRadius : 10
	});
}

// 停止加载动画
function stopLoader() {
	$('#loader').shCircleLoader("destroy");
}

// 退出网站
function logout() {
	$.ajax({
		url : 'user/unsubscribe',
		type : "GET",
		success : function(data) {
			window.location.href = "login.html";
		}
	});
}

// 删除帖子
function deletePost(postid, fun) {
	$.ajax({
		url : 'post/deletePost',
		type : "POST",
		data : {
			postid : postid
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				break;
			default:
				alert(data['msg']);
				break;
			}
		}
	});
}

// 获取帖子总数
function getPostsTotalNumber(postType, type, userid) {
	if (postType == 0) {
		var url = 'post/getUserPostsTotalNumber';
	} else if (postType == 1) {
		var url = 'post/getUserAgreeTotalNumber';
	} else if (postType == 2) {
		var url = 'post/getUserCollectTotalNumber';
	}
	var num = 0;
	$.ajax({
		url : url,
		type : "POST",
		async : false,
		data : {
			type : type,
			userid : userid
		},
		success : function(data) {
			console.log(data);
			switch (data['status']) {
			case -250:
				window.location.href = "login.html";
				break;
			case 0:
				num = data['result']['totalNum'];
				break;
			default:
				alert(data['msg']);
				break;
			}
		}
	});
	return num;
}